package com.haixiaoke.saas.order.state.role;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.haixiaoke.saas.common.constant.OrderConstants;
import com.haixiaoke.saas.common.enums.OrderStateEnum;
import com.haixiaoke.saas.common.enums.OrderTabEnum;
import com.haixiaoke.saas.common.utils.DateUtils;
import com.haixiaoke.saas.common.utils.bean.BeanUtils;
import com.haixiaoke.saas.house.domain.House;
import com.haixiaoke.saas.houseMember.domain.HouseMember;
import com.haixiaoke.saas.order.domain.Order;
import com.haixiaoke.saas.order.domain.OrderAuditRecord;
import com.haixiaoke.saas.order.domain.OrderLessee;
import com.haixiaoke.saas.order.state.server.IOrderStateServerRole;
import org.apache.commons.lang3.RandomUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

import static com.haixiaoke.saas.system.framework.security.util.SecurityUtils.getLoginUser;

/**
 * 续租状态服务方实现类
 *
 * @author qixi
 */
public class ReletOrderOrderStateServerRole extends AbstractSpecificOrderOrderStateServerRole {
    private static final Logger LOG = LoggerFactory.getLogger(ReletOrderOrderStateServerRole.class);

    public ReletOrderOrderStateServerRole(IOrderStateServerRole stateServerRole) {
        super(stateServerRole);
    }


    @Override
    @Transactional
    public void relet(Order order) {
        Order newOrder = order.getOrder();
        String houseId = newOrder.getHouseId();
        House house = houseService.selectHouseById(houseId);
        BeanUtil.copyProperties(house, newOrder);
        String estateName = house.getEstateName();
        String buildingName = house.getBuildingName();
        newOrder.setPremisesAddress(estateName + buildingName + "栋" + house.getUnitName() + "单元" + house.getRoomNumber() + "房");

        BeanUtils.copyPropertiesIfNull(order, newOrder, "order");
        String orderId = "O" + System.currentTimeMillis() + RandomUtils.nextInt(10000, 100000);
        newOrder.setOrderId(orderId);
        newOrder.setOrderState(OrderStateEnum.ORDER_STATE_TO_BE_SIGNED.getValue());
        newOrder.setOrderTab(OrderTabEnum.ORDER_TAB_TO_BE_SIGNED.getValue());
        newOrder.setOrderType(Order.ORDER_TYPE_LONG_LEASE);
        newOrder.setLinkedOrderId(order.getOrderId());
        newOrder.setCreateTime(new Date());
        newOrder.setDeptId(getLoginUser().getDeptId());
        newOrder.setCreateBy(getLoginUser().getUsername());
        newOrder.setTradingMode(OrderConstants.TRADING_MODE_RELET);
        newOrder.setFinallySignTime(null);
        orderService.insertOrder(newOrder);

        OrderAuditRecord orderAuditRecord = orderAuditRecordService.selectOrderAuditRecordByOrderIdAndAuditRecordState(order.getOrderId(), OrderConstants.AUDIT_RECORD_STATE_AUDIT);
        if (ObjectUtil.isNotNull(orderAuditRecord)) {
            orderAuditRecord.setAfterOrderId(newOrder.getOrderId());
            orderAuditRecordService.updateOrderAuditRecord(orderAuditRecord);
        }
/*
        OrderLessee orderLessee = newOrder.getOrderLessee();
        orderLessee.setOrderId(newOrder.getOrderId());
        orderLessee.setHouseId(newOrder.getHouseId());
        orderLessee.setTenantId(newOrder.getTenantId());
        orderLessee.setLesseeState(OrderLessee.LESSEE_STATE_AFOOT);
        orderLesseeService.insertOrderLessee(orderLessee);

        List<HouseMember> houseMembers = houseMemberService.selectHouseMemberListByOrderId(order.getOrderId());

        boolean lesseeFlag = false;
        if (CollectionUtil.isNotEmpty(houseMembers)) {
            for (HouseMember x : houseMembers) {
                if (x.getMemberType().equals(HouseMember.MEMBER_TYPE_TENANTRY)) {
                    // 承租人非同一人
                    if (!orderLessee.getCardNumber().equals(x.getIdCard())) {
                        lesseeFlag = true;
                        continue;
                    }
                }
                HouseMember houseMember = new HouseMember();
                BeanUtil.copyProperties(x, houseMember);
                houseMember.setOrderId(newOrder.getOrderId());
                houseMember.setMemberId(null);
                houseMemberService.insertHouseMember(houseMember);
            }
        }

        // 新增承租人
        if (lesseeFlag) {
            HouseMember houseMember = new HouseMember();
            houseMember.setTenantId(newOrder.getTenantId());
            houseMember.setDeptId(newOrder.getDeptId());
            houseMember.setHouseId(newOrder.getHouseId());
            houseMember.setMemberType(HouseMember.MEMBER_TYPE_TENANTRY);
            houseMember.setMemberName(orderLessee.getLesseeName());
            houseMember.setMemberPhone(orderLessee.getPhone());
            houseMember.setIdCard(orderLessee.getCardNumber());
            houseMember.setIdCardImgs(orderLessee.getCardImgs());
            houseMember.setOrderId(newOrder.getOrderId());
            houseMemberService.insertHouseMember(houseMember);
        }*/


        order.setOrderState(OrderStateEnum.ORDER_STATE_SIGNED.getValue());
        orderService.updateOrder(order);
    }


    @Override
    public void approved(Order order) {
        this.relet(order);
    }

    @Override
    public void disagree(Order order) {

        // 原始订单
        Order originalOrder = orderService.selectOrderByOrderId(order.getOrderId());
        originalOrder.setOrderState(OrderStateEnum.ORDER_STATE_SIGNED.getValue());

        addOrderStateRecord(originalOrder.getOrderId(), order.getOrderState(), OrderStateEnum.ORDER_STATE_SIGNED.getValue());
        orderService.updateOrder(originalOrder);
    }
}
